package com.guigu.qingcheng.orderservice.controller;


import com.guigu.qingcheng.orderservice.service.TbCategoryReportService;
import com.guigu.qingcheng.pojo.order.TbCategoryReport;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("tbcategoryreport")
public class TbCategoryReportController {
    @Autowired
    private TbCategoryReportService TbCategoryReportService;

    @GetMapping("findById")
    public R findById (Integer id) {
        TbCategoryReport tbCategoryReport = TbCategoryReportService.getById(id);
        if (tbCategoryReport  == null)
            return R.error();
        return R.ok().data("tbCategoryReport",tbCategoryReport);
    }

    //查询分页查询
    @PostMapping("findPage")
    public R findPage (@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody(required = false) Map<String,Object> searchMap ) {
        return TbCategoryReportService.findPage(page, size, searchMap);
    }

    //新增
    @PostMapping("add")
    public R add (@RequestBody TbCategoryReport tbCategoryReport) {
        boolean save = TbCategoryReportService.save(tbCategoryReport);
        if (save)
            return R.ok();
        return R.error();
    }

    //修改
    @PostMapping("update")
    public R update (@RequestBody TbCategoryReport tbCategoryReport) {
        boolean save = TbCategoryReportService.updateById(tbCategoryReport);
        if (save)
            return R.ok();
        return R.error();
    }

    //删除
    @GetMapping("delete")
    public R delete (String id) {
        boolean save = TbCategoryReportService.removeById(id);
        if (save)
            return R.ok();
        return R.error();
    }

    @GetMapping("/yesterday")
    public R yesterday(){
        LocalDate localDate =LocalDate.now().minusDays(1);
        List<TbCategoryReport> tbCategoryReports = TbCategoryReportService.categoryReports(localDate);
        return R.ok().data("list",tbCategoryReports);
    }

    @GetMapping("/category1Count")
    public R category1Count(String date1, String date2){
        List<Map> maps = TbCategoryReportService.category1Count(date1, date2);
        return R.ok().data("list",maps);
    }

}

